package com.controller;

import com.entity.Users;
import com.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

@Controller
public class RegisterController {
    @Autowired
    UsersService usersService;
    //注册
    @GetMapping(value = "/register")
    public String register(){
        return "client/register";
    }
    //提交注册
    @PostMapping(value = "/toRegister")
    public String toRegister(Users fUser, Model model){
        try {
            boolean row = usersService.save(fUser);//添加用户
            if (row) {
                model.addAttribute("msg", "注册成功,请登陆！");
                return "client/login";
            } else {
                model.addAttribute("error","注册失败，服务器出错请联系管理员");
            }
        }catch (Exception ex){
            model.addAttribute("error","注册失败，已存在"+fUser.getUsername());
        }
        return "client/register";
    }
}
